package com.xcity.game.item.effect;

import com.xcity.db.entity.Player;
import com.xcity.db.entity.Relation2;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.item.GameItem;
import com.xcity.game.item.ItemEffect;
import com.xcity.game.item.ItemException;
import com.xcity.game.item.listener.BagListener;
import com.xcity.game.relation.RelationService;
import com.xcity.game.relation.Relations;

import naga.x.App;

/**
 * 增加好友之间的亲密度
 * @author YangLi
 *
 */
public class AddFriendIntimacyEffect implements ItemEffect {

	private RelationService service;
	
	@Override
	public boolean effect(Player player, GameItem item, int count, BagListener listener) throws ItemException {
		Player source = item.getBag().getOwner();
		Relations rels = getRelationService().findRelations(source);
		Relation2 rel = rels.find(Relation2.TYPE_FRIEND, player.getId());
		if (rel == null) {
			throw new ItemException(MessageConst.ITEM_USE_ONLY_FOR_FRIEND);
		}
		rel.addIntimacy(item.getTemplate().getParameter(0), Cause.USE_ITEM);
		return true;
	}

	private RelationService getRelationService() {
		return service != null ? service : (service = App.getApp().getServiceManager().get(RelationService.class));
	}
	
}
